package cipher.module;

import java.io.IOException;
import java.util.ArrayList;

import cipher.algorithm.Algorithm;

public class Cipher_Thread_Manager {
	
	public Cipher_Thread_Manager() {}

	public byte[] chiffrement(Algorithm algo, byte[] in, int mode, int cores) throws IOException, InterruptedException {
		int size = in.length/cores;
		ArrayList<Cipher_Thread> list = new ArrayList<>();
		byte[] out = new byte[in.length];
		//System.out.println("sub "+size);
		
		for(int i=0; i<cores; i++) {
			byte[] buf = new byte[size];
			System.arraycopy(in, i*size, buf, 0, size);
			Cipher_Thread ct = new Cipher_Thread(algo, mode, buf, i);
			list.add(ct);
			ct.start();
		}
		
		for(int i=0; i<cores; i++) {
			Cipher_Thread ct = list.get(i);
			ct.join();
			System.arraycopy(ct.getData(), 0, out, i*size, size);
		}

		System.out.println("> "+in[15000]+" "+out[15000]);
		
		return out;
	}
	
}
